<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - AI公众号助手</title>
    <link rel="stylesheet" href="/styles.css">
</head>
<body>
    <div class="container">
        <div class="login-container">
            <div class="login-box">
                <a href="/" class="back-home">← 返回首页</a>
                <h2>创建账号</h2>
                <p class="login-subtitle">AI公众号助手</p>
                <form id="registerForm" class="login-form">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username" placeholder="请输入用户名" required>
                        <p class="form-tip">用户名长度为4-20个字符，只能包含字母、数字和下划线</p>
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" name="password" placeholder="请输入密码" required>
                        <p class="form-tip">密码长度至少8位，必须包含字母和数字</p>
                    </div>
                    <div class="form-group">
                        <label for="confirmPassword">确认密码</label>
                        <input type="password" id="confirmPassword" name="confirmPassword" placeholder="请再次输入密码" required>
                    </div>
                    <div class="form-group">
                        <label for="email">邮箱</label>
                        <input type="email" id="email" name="email" placeholder="请输入邮箱" required>
                        <p class="form-tip">请输入有效的邮箱地址，用于接收验证码和找回密码</p>
                    </div>
                    <button type="submit" class="login-submit-btn">注册</button>
                    <div class="register-link">
                        <span>已有账号？</span>
                        <a href="/login" class="register-btn">立即登录</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        // 表单验证
        document.getElementById('registerForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const email = document.getElementById('email').value;

            // 验证密码是否匹配
            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }

            // 发送注册请求
            fetch('/user/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({
                    username: username,
                    password: password,
                    email: email
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('注册成功！');
                    if (data.redirectUrl) {
                        window.location.href = data.redirectUrl;
                    } else {
                        window.location.href = '/login';
                    }
                } else {
                    alert(data.message || '注册失败，请稍后重试');
                }
            })
            .catch(error => {
                console.error('注册请求失败:', error);
                alert('注册请求失败，请稍后重试');
            });
        });
    </script>
</body>
</html> 